# _*_ coding:utf-8 _*_
import os
import io
import json
import pymysql


coon = pymysql.connect(host='127.0.0.1', user='root',
                       passwd='fan0314@', port=3306, db='fly_info', charset='utf8')
cur = coon.cursor()  # 建立游标

def executeSql(sql, data):
    cur.execute(sql, data)
    coon.commit()


def insert(data):
    sql = 'INSERT INTO `fly_info`.`airport`(`city`, `name`, `pinyin`, `code`, `distance`, `province`) VALUES (%s, %s, %s, %s, %s, %s);'
    executeSql(sql, data)

def getJiChang():
    f = open("data/jichang.json")
    all_jichang = f.read()
    all_jichang_json = json.loads(all_jichang)
    return all_jichang_json['data']

def jichangToDB(jc):
    print(jc)
    city = jc['city']
    name = jc['name']
    pinyin = jc['pinyin']
    code = jc['code']
    distance = jc['distance']
    province = jc['province']
    data = (city, name, pinyin, code, distance, province)
    insert(data)

if __name__ == "__main__":
    ls = getJiChang()
    for l in ls:
        jichangToDB(l)